from core.utils.analysis_platform_id_tool import AnalysisPlatformIdTool


class AnalysisStructureService:
    def __init__(self):
        pass

    @staticmethod
    def analysis_list_first_structure(response, category):
        """
        解析 传媒 类型的网页结构
        :param response: 响应结构
        :param category: 新闻类型
        :return: 返回结果
        """
        divs = response.xpath("//div[@class='list_message']//div[contains(@class,'news_one')]")
        data_list = list()
        for div in divs:
            news_title = div.xpath("./div/div/h2/a/text()").get()
            href = div.xpath("./div/div/h2/a/@href").get()
            follower = div.xpath("./div/div/div/div[2]/a/text()").get()
            pic_href = div.xpath("./div/a/img/@src").get()
            news_id = AnalysisPlatformIdTool.re_get_id(href)
            keywords_list = list()

            # print(news_id, title, follower, pic_href, category, href, keywords_list)
            data_list.append(
                {'news_id': news_id, 'news_title': news_title, 'follower': follower, 'pic_href': pic_href, 'category': category,
                 'href': href, 'keywords': keywords_list})
        return data_list

    @staticmethod
    def analysis_detail_first_structure(response, news_id):
        """
        解析新闻的详情页
        :param response: 响应结构
        :param news_id: 新闻编号
        :return: 返回结果
        """
        news_title = response.xpath("//h1[@class='post_title']/text()").get()
        try:
            news_time = response.xpath("//div[@class='post_info']/text()").get().strip()[:19]
        except:
            try:
                news_time = response.xpath("//span[@class='ptime']/text()").get().strip()
            except:
                news_time = None
        try:
            publisher_location = response.xpath(
                "//div[@class='post_info']/img/following-sibling::text()[1]").get().strip()
        except:
            publisher_location = response.xpath("//span[@class='iplocation']/text").get()

        publisher_name = response.xpath("//div[@class='post_info']/a/text()").get()
        publisher_total_article = response.xpath("//span[@class='post_wemedia_info1']/a/em/text()").get()
        publisher_total_fans = response.xpath("//span[@class='post_wemedia_info2']/a/em/text()").get()
        publisher_url = response.xpath("//div[@class='post_wemedia_name']/a/@href").get()
        news_detail = (''.join(response.xpath("//div[@class='post_body']//text()").getall())
                       .replace("\n", "")
                       .replace("\r", "")
                       .replace(" ", ""))
        try:
            news_detail_body = response.xpath("//div[@class='post_body']").get().replace("\n", "")
        except:
            news_detail_body = response.xpath("//div[@class='post_body']").get()

        return {'news_id': news_id, 'news_title': news_title, 'news_time': news_time,
                'publisher_name': publisher_name,
                'publisher_url': publisher_url, 'publisher_location': publisher_location,
                'publisher_total_article': publisher_total_article,
                'publisher_total_fans': publisher_total_fans, 'news_detail': news_detail,
                'news_detail_body': news_detail_body}